<?php
/**
 * Created by PhpStorm.
 * User: ysw
 * Date: 2020/6/29
 * Time: 10:31
 */

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;
use app\api\service\CreateVoteService;
use app\api\validate\CreateVoteValidate;
use think\response\Json;
use app\api\model\CreateVoteModel;

class Createvote extends Api
{
    protected $noNeedLogin = ['hotVoteList'];

    protected $noNeedRight = ['*'];

    /**
     * 添加投票服务
     * @var CreateVoteService
     */
    protected $createVoteService;

    /**
     * 验证投票数据
     * @var CreateVoteValidate
     */
    protected $createVoteValidate;

    /**
     * 构造方法
     * CreateVote constructor.
     * @param Request|null $request
     */
    public function __construct(Request $request = null)
    {
        $this->createVoteService = new CreateVoteService();

        $this->createVoteValidate = new CreateVoteValidate();

        parent::__construct($request);
    }

    /**
     * 添加投票
     * @access public
     * @param string title
     * @param string rule
     * @param int deadline
     * @param int vote_num
     * @param int vote_num_refresh
     * @return Json
     */
    public function addCreateVote(){
        $post = $this->request->post();

        $getUserinfo = $this->auth->getUserinfo();
        $res = $this->createVoteValidate->scene('createVote')->check($post);
        if(!$res){
            $this->error($this->createVoteValidate->getError());
        }
        $post['create_vote_describe'] = json_decode(htmlspecialchars_decode($post['create_vote_describe']),true);
        if (!isset($post['create_vote_describe']) || empty($post['create_vote_describe'])){
            $this->error('投票选项格式不正确');
        }
        $res = $this->createVoteService->addCreateVote($post,$getUserinfo);
        if ($res['code'] == 0){
            $this->error($res['msg']);
        }else{
            $this->success('添加投票成功',['create_vote_id'=>$res['data']]);
        }
    }


    /**
     * 投票列表
     * @param int create_vote_id
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function joinSelfVote(){
        $create_vote_id = $this->request->post('create_vote_id');
        $getUserinfo = $this->auth->getUserinfo();
        $res = $this->createVoteService->createVoteList($getUserinfo,$create_vote_id);
        if ($res['code']==0){
            $this->error($res['msg']);
        }else{
            $this->success($res['msg'],$res['data']);
        }
    }

    /**
     * 用户投票
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addUserVote(){
        $getUserinfo = $this->auth->getUserinfo();
        $post = $this->request->post();
        $res = $this->createVoteValidate->scene('addUserVote')->check($post);
        if(!$res){
            $this->error($this->createVoteValidate->getError());
        }
        $res = $this->createVoteService->addUserVote($getUserinfo,$post);
        if ($res['code']==0){
            $this->error($res['msg']);
        }else{
            $this->success($res['msg']);
        }
    }

    /**
     * 热门投票
     */
    public function hotVoteList(){
        $res = $this->createVoteService->hotVoteList(2);
        if ($res['code'] == 0){
            $this->error('暂无数据');
        }else{
            $this->success('获取数据成功',$res['data']);
        }
    }

    /**
     * 今日热门投票
     */
    public function hotVoteListToday(){
        $res = $this->createVoteService->hotVoteListToday(20);
        if ($res['code'] == 0){
            $this->error('暂无数据');
        }else{
            $this->success('获取数据成功',$res['data']);
        }
    }

    /**
     * 拍视频回调
     */
    public function videoNotify(){
        $getUserinfo = $this->auth->getUserinfo();
        $post = $this->request->post();
        $res = $this->createVoteValidate->scene('videoNotify')->check($post);
        if(!$res){
            $this->error($this->createVoteValidate->getError());
        }
        $res = $this->createVoteService->videoNotify($getUserinfo,$post);

        if ($res['code'] == 0){
            $this->error($res['msg']);
        }else{
            $this->success($res['msg']);
        }
    }

    /**
     * 分享回调
     */
    public function shareNotify(){
        $getUserinfo = $this->auth->getUserinfo();
        $post = $this->request->post();
        $res = $this->createVoteValidate->scene('shareNotify')->check($post);
        if(!$res){
            $this->error($this->createVoteValidate->getError());
        }

        if ($post['share_address']=='undefined' || $post['share_address'] == "null " || $post['share_address'] == null){
            $this->error('分享视频地址不合法');
        }
        $res = $this->createVoteService->shareNotify($getUserinfo,$post);
        if ($res['code'] == 0){
            $this->error($res['msg']);
        }else{
            $this->success('分享成功',$res['data']);
        }
    }

    /**
     * 删除自己的投票
     */
    public function myDelVote(){
        $getUserinfo = $this->auth->getUserinfo();
        $post = $this->request->post();

        $res = $this->createVoteValidate->scene('myDelVote')->check($post);
        if(!$res){
            $this->error($this->createVoteValidate->getError());
        }
        $res = $this->createVoteService->myDelVote($getUserinfo,$post);
        if ($res['code'] == 0){
            $this->error($res['msg']);
        }else{
            $this->success($res['msg']);
        }
    }
}